﻿using AutoMapper;
using B.S.Warehouse.Domain;
using B.S.Warehouse.ErrorCord;
using B.S.Warehouse.Write.Api.Application.Command.WarehouseManagementCommand;
using MediatR;
using SqlSugar;

namespace B.S.Warehouse.Write.Api.Application.Handelr.WarehouseManagementCommandHandler
{
    public class AddStorageAreaCommandHandler : IRequestHandler<AddStorageAreaCommand, ApiResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public AddStorageAreaCommandHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<ApiResult<object>> Handle(AddStorageAreaCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();

            result.Code = ApiEnum.添加成功;
            result.MAG = ApiEnum.添加成功.ToString();
            var model = mapper.Map<StorageArea>(request);
            string yeartime = DateTime.Now.ToString("yyyyMMddHHmmss");
            string ProductCodess1 = "KW" + yeartime;
            model.StorageAreaCode = ProductCodess1;

            await db.Insertable(model).ExecuteCommandAsync();

            return await Task.FromResult(result);
        }
    }
}
